<template>
  <!-- // 分配角色 -->
  <el-dialog title="分配角色" :visible.sync="isShow" @close="close">
    <el-checkbox-group v-model="roleIds">
      <el-checkbox v-for="item in list" :key="item.id" :label="item.id">{{
        item.name
      }}</el-checkbox>
    </el-checkbox-group>

    <div slot="footer">
      <el-button @click="isShow = false">取 消</el-button>
      <el-button type="primary" @click="benOK">确 定</el-button>
    </div>
  </el-dialog>
</template>
<script>
import { getRoles } from '@/api/role'
import { getUserDetailById } from '@/api/user'
import { assignRoles } from '@/api/employees'

export default {
  data() {
    return {
      isShow: false,
      roleIds: [],
      list: [],
      curId: ''
    }
  },
  methods: {
    // 初始化数据
    async initRoles() {
      const { data } = await getRoles({ page: 1, pagesize: 100 })
      this.list = data.rows
    },
    // 根据用户id获取用户角色
    async getRoleById(id) {
      this.curId = id
      const { data } = await getUserDetailById(id)
      this.roleIds = data.roleIds
    },
    // 点击确定按钮
    async benOK() {
      await assignRoles({ id: this.curId, roleIds: this.roleIds })
      this.$message.success('分配成功')
      this.isShow = false
    },
    // 关闭弹框，重置数据
    close() {
      this.roleIds = []
      this.list = []
      this.curId = ''
    }
  }
}
</script>
